﻿<?php
require_once('includes/inc.php');
checkLang($_GET['j']);
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
$ipaddr=$_SERVER['REMOTE_ADDR'];

// proverka za ip adresa - dokolku se posetuva lokalno so cel da se vidi funkcionalnosta na prikazuvanje uslovi spored lokacijata
if(!contains("192.168.",$ipaddr)) $ipaddr="79.126.231.157";

/* zapis vo log */
$ip=$_SERVER["REMOTE_ADDR"];
$datum = date("F j, Y, g:i a");   
$ip.=" - $datum\r\n";
$log = "iplog.txt";
$fh = fopen($log, 'a') or die("err");
fwrite($fh, $ip);
fclose($fh);
/* kraj na zapis vo log */

$grad=trim($_GET['grad']);
$unit=$_GET['unit'];
if($unit != "C" && $unit != "F") $unit="C";


$pattern = "/[0'?;\/#&_+]/";
if (preg_match($pattern,$grad) || $grad==" " || $grad=="") $errval=1;
else $errval=0;


$akey = getAKey();

$grad=str_replace(" ","%20",$grad);

$link="http://where.yahooapis.com/v1/places.q('".$grad."')?appid=".$akey;

if($errval) { echo "<b>"._ERR."</b>"; return false; }


$xmlmap = simplexml_load_file($link);


$xxx = $xmlmap->place;

$woeid = $xxx->woeid;

$long = $xxx->centroid->longitude;

$lat = $xxx->centroid->latitude;

$u = strtolower($unit);
$visited=" class=\"unitvisit\" ";

switch($u)
{
	case 'c':
		$unit1="mb";
		$unit2="Km";
		$unit3="Kph";
		break;
	case 'f':
		$unit1="in";
		$unit2="Mi";
		$unit3="Mph";
		break;
}

	//zname za jazik
	
	$lflag = getRFlag();
	
	// source link
	$link="http://weather.yahooapis.com/forecastrss?w=".$woeid."&u=".$u;
	
	// zemanje na parametri od
	$src=file_get_contents($link);
	
	if(!(contains("Error",$src)))
	{
	
	//grad
	$grad=getAttrib("location","city",$src);
	
	//temperatura
	$temp=getAttrib("condition","temp",$src);
	
	//pritisok
	$pritisok=getAttrib("atmosphere","pressure",$src);
	
	//vlaznost
	$vlaznost=getAttrib("atmosphere","humidity",$src);
	
	//vidlivost
	$vid=getAttrib("atmosphere","visibility",$src);
	
	//vetar
	$vetar=getAttrib("wind","speed",$src);
	
	//kod za vremenski uslovi
	$kod=getAttrib("condition","code",$src);
	
	// vremenska sostojba
	$tempcond=getFlag();
	switch($tempcond)
		{
			case 'mk': $cond=getCond($kod,'mak'); break;
			case 'en': $cond=ucfirst(getCond($kod,'eng')); break;
			default: $cond=getCond($kod,'mak'); break;
		}
	}
	else 
	{
		$pritisok="";
		$vlaznost="";
		$vid="";
		$vetar="";
		$temp="";
		$kod="";
		$slika="";
		$cond="";
	}
	if($pritisok=="") $pritisok="N/A";
	if($temp=="") $temp="N/A";
	if($vid=="") $vid="N/A";
	if($vetar=="") $vetar="N/A";
	if($vlaznost=="") $vlaznost="N/A";	
	if($kod=="") $kod="NA";
	if($vid>="999") { $vid=_CLEAR; $unit2=""; }
	
	// slika soodvetno na uslovite
	$slika="img/$kod.png";
	
	// tekovno vreme
	$vreme=date("H:i");
	
	
	// ako imeto na gradot e pogolemo od 18 karakteri skrati go i dodadi ...
	$grad=str_replace('%20',' ',$grad);
	$gradfull=$grad;
	if(strlen($grad)>18) 
	{
		$grad=substr($grad,0,18);
		$grad.="...";
	}
	
echo "	
<div id=\"jwmain\">
		<div id=\"jwheader\">
			<div id=\"jwgrad\" title=\"".$gradfull."\">".$grad." </div>
			<div id=\"jwcas\">| ".$vreme." h</div>
			<div id=\"jwlang\">
			<a href=\"index.php?j=".substr($lflag,4,2)."\"<img src=\"$lflag\" onclick=\"javascript:getWeather();\"></img></a>
			</div>
			<div id=\"jwunit\">";
			if($u=='f') echo "
			<a href=\"#\" title=\""._KONVERTIRAJC."\" onclick=\"javascript:convert('C');\">°C</a> |";
			if($u=='c') echo " 
			<a href=\"#\" title=\""._KONVERTIRAJF."\"  onclick=\"javascript:convert('F');\">°F</a> |";
			echo "
			</div>
		</div>
		<div id=\"jwcontent\">
			<div id=\"jwmain1\">
				<div id=\"jwlogo\"><img src=".$slika." alt=\"".$cond."\" title=\"".$cond."\"></div>
				<div id=\"jwtext\">".$cond."<br /> <span id=\"jwtemp\">".$temp." °".$unit."</span></div>
			</div>
			<div id=\"jwmain2\">
				<div id=\"left\">
						<span id=\"l1\">"._PRITISOK.": <span id=\"jwpritisok\">".$pritisok." ".$unit1."</span></span><br />
						<span id=\"l1\">"._VIDLIVOST.": <span id=\"jwvid\">".$vid." ".$unit2."</span></span><br />
				</div>
				<div id=\"right\">					
						<span id=\"l1\">"._VLAZNOST.": ".$vlaznost." %</span> <br />
						<span id=\"l1\">"._VETER.": <span id=\"jwwind\">".$vetar." ".$unit3."</span></span> <br />
					
				</div>
			</div>
			<div id=\"jwmain3\">
				<a href=\"map.php?long=".$long."&lat=".$lat."&temp=".$temp."&kod=".$kod."&cond=".$cond."\"
				toptions=\"width = 400, height = 320, type = iframe, layout = dashboard, resizable=0\">"._MAPA."</a> | <a href=\"istorija.php\" 
				toptions=\"width = 640, height = 480, type = iframe, layout = dashboard, resizable=1\">"._ISTORIJA."</a>
			</div>
		</div>
	</div>
";
?>